#ifndef API_H
#define API_H
#include <QObject>
#include <QDateTime>
#include <QQmlEngine>
#include <QJSEngine>
#include <QtQuick>

class Api : public QObject
{
    Q_OBJECT
public:
    Api(QObject* parent = 0);
    virtual ~Api();
public slots:
    QString __newGUID();
    static QString newGUID();

    QString sslRandom(int len);

    bool isValid(const QDateTime &datetime);
    bool isValid(const QUrl &url);

    qint64 diffTime(const QDateTime &d1, const QDateTime &d2, const QString &f);

    QDateTime addTime(const QDateTime &dm, int n, const QString &f);

    //QString toLocalFile(const QUrl &fileUrl);
    //QUrl toUrl(const QString &fileName);

    QDateTime lastModified(const QString &fileName);

    QString fileName(const QString &filePath);

    //int buildVersion();

    int fileSize(const QString fileName);

    QStringList macAddress();
    QString folder(const QString &filePath) const;

    QString localHostName();
    static QString LocalHostName();
    QVariantMap urlInfo(const QUrl &url);
    QVariantMap mailboxInfo(const QString &mb);

    QString fragment(const QUrl &url, QUrl::ComponentFormattingOptions options = QUrl::PrettyDecoded) const;
    QUrl setFragment(const QUrl &url, const QString & fragment, QUrl::ParsingMode mode = QUrl::TolerantMode);
    bool hasFragment(const QUrl &url) const;

    QUrl addQuery(const QUrl &url, const QVariantMap &data);
    bool hasQuery(const QUrl &url) const;
    QString toPercentEncoding(const QString &keyword);

    QVariantMap query(const QUrl &url) const;
    QString toQueryString(const QVariantMap &d);

    QVariantMap iniData(const QString &fileName);

    QStringList filterFiles(const QString &path, const QStringList &filter);

    void keyReturnEvent(QQuickItem *item);

    void mouseClickEvent(QQuickItem *item, const QPoint &pos);

    void keyDownEvent(QQuickItem *item);

    int length(const QString &data);

    QString openFolderDialog(const QString &caption, const QString &dir);

    QString grabRect(const QRect &size);
    QString grabScreen(int x, int y, const QSize &size);
    QString grabWId(uint hWnd, int x, int y, const QSize &size);

    QString combinePixmap(const QStringList &pixmapData);

    QString zeroFill(qlonglong num, int fieldWidth);
    void closeTopWindow();


    //QString writableLocation(int index) const;
    void explorer(const QUrl &url);

    //bool mkpath(const QString &path);

    QString toBase64(const QString &filePath, const QSize &size = QSize(-1, -1));

    QDateTime toDateTime(const QString &datetime, const QString &format);

    //QStringList files(const QString &path);

    QPoint mapToGlobal(QWindow * window, const QPoint &pos) const;
    void setCurror(const QPoint &p);
    bool contains(const QRectF & rect, int x, int y);
public slots:


    void toFileImage(const QString &fileName, const QString &data);

    void updateVersion(const QString &fileName);

    void restart(const QStringList &arguments);

    //QString downloadLocation() const;

    QStringList readAllLine(const QString &fileName);

    QString stringify(const QVariantMap & json);
    QVariantMap jsonParse(const QString &json);

    QString workPath() const;
    //bool fileExist(const QString &fileName) const;
    bool pathExist(const QString &path) const;


    bool isImage(const QUrl &url) const;

    void createTreeModel(const QString &name);
    QString __fileMD5(const QString &fileName);

    void __textToClipboard(const QString &text);
    void __imageToClipboard(const QString &text);
    QString __imageFileBase64(const QString & fileName, const QSize &size = QSize(-1, -1));
    bool __isSupportedImage(const QString & fileName);

    QString favicon();
    bool isValidImage(const QString &imageData);

    QString key(const QUrl & url);
public:
    static QString fileMD5(const QString &fileName);
    static QHostAddress activeHostAddress();
    static void textToClipboard(const QString &text);
    static void imageToClipboard(const QString &text);

    static QStringList supportedImages();

    static QString encrypt(const QString &key, const QString &data);
    static QString decrypt(const QString &key, const QString &data);
    static QString HexMD5(const QString &data);


    static bool IsValidImage(const QString &imageData);


protected:
    QMimeDatabase m_mime;
};

extern QObject *Api_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine);

#endif // API_H
